/*
 * 文件名：ProtocalSdtpDataAcceptClient.java
 * 版权：亚信科技(南京)版权所有
 * 描述：TODO
 * 修改人：xuwei3
 * 修改时间：2015-11-5
 * 修改内容：TODO
 */
package com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.server;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ailk.bigdata.etl.common.server.constant.Constant;
import com.ailk.bigdata.etl.realstream.server.constant.ProtocalSdtpConstant;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.HeartBeatReqHandler;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.LoginAuthReqHandler;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.ProtocalSdtpDataDecode;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.ProtocalSdtpDataEncode;
import com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler.TestDataSendHandler;

/**
 * @description sdtp模拟客户端测试
 * @author [xuwei3]
 * @version [版本号,2015-11-5]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ProtocalSdtpDataAcceptClient
{

	private static final Logger logger = LoggerFactory.getLogger(ProtocalSdtpDataAcceptClient.class);
	private String host;
	private int port;

	public ProtocalSdtpDataAcceptClient(String host, int port)
	{
		this.host = host;
		this.port = port;
	}

	public int run(final ChannelInboundHandlerAdapter handler)
	{
		EventLoopGroup group = new NioEventLoopGroup();
		Bootstrap b = new Bootstrap();
		try
		{
			b.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, false).option(ChannelOption.TCP_NODELAY, true)
					.handler(new LoggingHandler(LogLevel.INFO)).handler(new ChannelInitializer<SocketChannel>()
					{
						@Override
						public void initChannel(SocketChannel ch) throws Exception
						{
							ch.pipeline()
									.addLast("messToByte", new ProtocalSdtpDataEncode())
									.addLast("byteToMess", new ProtocalSdtpDataDecode())
									.addLast("readTimeoutHandler",
											new ReadTimeoutHandler(ProtocalSdtpConstant.SDTP_PROTOCAL_HEART_BEAT_INTERVAL, TimeUnit.SECONDS))
									.addLast("loginAuthHandler", new LoginAuthReqHandler()).addLast("heartBeatReq", new HeartBeatReqHandler())
									.addLast("handler", new TestDataSendHandler());
						}
					});
			b.connect(host, port).sync().channel().closeFuture().sync();
		}
		catch (Exception e)
		{
			logger.error("连接失败！请确认host[" + host + "],port[" + port + "]是否配置正确", e);
		}
		finally
		{
			group.shutdownGracefully();
		}
		return Constant.RETURN_OK;
	}

	public static void main(String[] args)
	{
		new ProtocalSdtpDataAcceptClient("10.4.56.161", 17501).run(null);
	}
}
